var schoolID;
var teachersTable;

$(document).ready(function (){
	//cookie values
	var cookieValue=$.cookie("school");
	var cookieValueArray = cookieValue.split('/');
	schoolID = cookieValueArray[0];
	
	teachersTable=$('#tbl-teachers').dataTable(
		{
			"columnDefs": [
				{
					"targets": [ 0 ],
					"visible": false,
					"searchable": false
				}
			]
		}
	);

	$('#tbl-teachers tbody').on( 'click', 'tr',function(){
		var nTr = $(this);
		if ( teachersTable.fnIsOpen(nTr) )
        {
            teachersTable.fnClose( nTr );
            $('#tbl-teachers').addClass('table-hover');
        }
        else
        {
        	teachersTable.fnOpen( nTr, fnFormatDetails( teachersTable , nTr), 'details' );
        	$('#tbl-teachers').removeClass('table-hover');
        }
	});
	
});

function fnFormatDetails(table,row)
{
	var oData = teachersTable.fnGetData( row );
	teacherId = oData[0];	
	var out;
	var url="php/getTeacherDetail.php";
			
	$.ajax({
	url: url,
	type: 'GET',
	dataType: "json",
	data: 'id='+teacherId,
	success: function(data) {
		if(data.error=="false")
		{						
			//$('#id'+teacherId).val(data.list.id);
			$('#empno'+teacherId).val(data.list.empno);
			$('#name'+teacherId).val(data.list.name);
			$('#dob'+teacherId).val(data.list.dob);
			$('#eployment_date'+teacherId).val(data.list.eployment_date);
			$('#position'+teacherId).val(data.list.position);
			$('#adhar'+teacherId).val(data.list.adhar);
			$('#gender'+teacherId).val(data.list.gender);
			$('#maritial_status'+teacherId).val(data.list.maritial_status);
			$('#cell_number'+teacherId).val(data.list.cell_number);
			$('#landline'+teacherId).val(data.list.landline);
			$('#email'+teacherId).val(data.list.email);
			$('#village'+teacherId).val(data.list.village);
			$('#city'+teacherId).val(data.list.city);
			$('#district'+teacherId).val(data.list.district);
			$('#state'+teacherId).val(data.list.state);
			$('#zip'+teacherId).val(data.list.zip);
			$('#address'+teacherId).val(data.list.address);
			$('#degree'+teacherId).val(data.list.degree);
			$('#certificate'+teacherId).val(data.list.certificate);
			$('#experience'+teacherId).val(data.list.experience);
			//$('#school_id'+teacherId).val(data.list.school_id);
			
			if(data.list.image)
				$('#image'+teacherId).attr('src', data.list.image);

		}
	},
	error: function(e) {
		console.debug("error");
	}
	});
	
	
	out = 	'<div style="border: 1px solid #ddd; padding:10px;">'+
					'<div role="tabpanel">'+
						'<ul class="nav nav-tabs" role="tablist">'+
				    		'<li role="presentation" class="active"><a href="#staff_info" aria-controls="home" role="tab" data-toggle="tab">Staff Info</a></li>'+
					    	'<li role="presentation"><a href="#staff_contact_details" aria-controls="profile" role="tab" data-toggle="tab">Contact Details</a></li>'+
					    	'<li role="presentation"><a href="#staff_qual" aria-controls="messages" role="tab" data-toggle="tab">Qualification</a></li>'+
					  	'</ul>'+
					  	'<div class="tab-content">'+
				    		'<div role="tabpanel" class="tab-pane fade in active" id="staff_info">'+
				    			'<div class="row">'+
								    '<div class="col-md-3"><img width="70%" src="images/default_user.png" style="margin: auto;border: 1px solid #ddd;box-shadow: 0px 0px 5px 0px;border-radius: 10px;margin: 15%" id="image'+teacherId+'"><input type="file" onchange="fileinput(\'imageInput'+teacherId+'\','+teacherId+');" id="imageInput'+teacherId+'"></div>'+
								    '<div class="col-md-9">'+
								      	'<div class="row" style="margin-top: 5%">'+
									      	'<div class="col-md-5">'+
									      		'<input type="text" style="margin-bottom:4%" placeholder="Employe No." class="form-control" id="empno'+teacherId+'">'+
												'<input type="text" style="margin-bottom:4%" placeholder="Emplye Name" class="form-control" id="name'+teacherId+'">'+
												'<input type="text" style="margin-bottom:4%" placeholder="Date Of Birth" class="form-control" id="dob'+teacherId+'">'+
												'<input type="text" style="margin-bottom:4%" placeholder="Gender" class="form-control" id="gender'+teacherId+'">'+
											'</div>'+
											'<div class="col-md-5">'+
												'<input type="text" style="margin-bottom:4%" placeholder="Employment Date" class="form-control" id="eployment_date'+teacherId+'">'+
												'<input type="text" style="margin-bottom:4%" placeholder="Position" class="form-control" id="position'+teacherId+'">'+
												'<input type="text" style="margin-bottom:4%" placeholder="Aadhar Card No" class="form-control" id="adhar'+teacherId+'">'+
												'<input type="text" style="margin-bottom:4%" placeholder="Marital status" class="form-control" id="maritial_status'+teacherId+'">'+
											'</div>'+
										'</div>'+
								    '</div>'+
								'</div>'+
					    	'</div>'+
						    '<div role="tabpanel" class="tab-pane fade" id="staff_contact_details">'+
						    	'<div class="row" style="margin-top: 3%">'+
								    '<div class="col-md-4">'+
								    	'<input type="text" style="margin-bottom:4%" placeholder="Cell Phone" class="form-control" id="cell_number'+teacherId+'">'+
								    	'<input type="text" style="margin-bottom:4%" placeholder="Land Phone" class="form-control" id="landline'+teacherId+'">'+
										'<input type="text" style="margin-bottom:4%" placeholder="E-mail" class="form-control" id="email'+teacherId+'">'+
									'</div>'+
								    '<div class="col-md-4">'+
								    	'<input type="text" style="margin-bottom:4%" placeholder="Village" class="form-control" id="village'+teacherId+'">'+
										'<input type="text" style="margin-bottom:4%" placeholder="City" class="form-control" id="city'+teacherId+'">'+
										'<input type="text" style="margin-bottom:4%" placeholder="District" class="form-control" id="district'+teacherId+'">'+
									'</div>'+
									'<div class="col-md-4">'+
								    	'<input type="text" style="margin-bottom:4%" placeholder="State" class="form-control" id="state'+teacherId+'">'+
										'<input type="text" style="margin-bottom:4%" placeholder="Zip Code" class="form-control" id="zip'+teacherId+'">'+
										'<textarea name="inputtext" placeholder="Residential Address" id="address'+teacherId+'">'+
								    	'</textarea>'+
									'</div>'+
								'</div>'+
						    '</div>'+
						    '<div role="tabpanel" class="tab-pane fade" id="staff_qual">'+
						    	'<div class="row" style="margin-top: 3%">'+
						    	    '<div class="col-md-4">'+
								    	'<input type="text" style="margin-bottom:4%" placeholder="Degree" class="form-control" id="degree'+teacherId+'">'+
								    '</div>'+
								    '<div class="col-md-4">'+
										'<input type="text" style="margin-bottom:4%" placeholder="Certification" class="form-control" id="certificate'+teacherId+'">'+
								    '</div>'+
								    '<div class="col-md-4">'+
										'<input type="text" style="margin-bottom:4%" placeholder="Experience" class="form-control" id="experience'+teacherId+'">'+
								    '</div>'+
								'</div>'+
						    '</div>'+
						    '<div class="row"><button type="button" class="btn btn-success" style="float:right;margin-right:150px;" onclick="updateTeacher('+teacherId+')">Save</button></div>'+
					  	'</div>'+
				  	'</div>'+
				'</div>';
	return out;
}

var uploadedTeacherImage;
function fileinput(id,teacherId)
{
	var uploadImage = jQuery('#'+id).get(0).files[0];
	var reader = new FileReader();
	reader.readAsDataURL(uploadImage);
	reader.onload = shipOff;
	function shipOff(event) {
		uploadedTeacherImage = event.target.result;		
		$("#image"+teacherId).attr("src", uploadedTeacherImage);
	}	
}

function saveTeacher(){	
	var empno = $('#txtempno').val();
	var name = $('#txtTeacherName').val();
	var gender = $('#txtTeacherGender').val();
	var position = $('#txtTeacherPosition').val();
	var eployment_date = $('#txtTeacherDoj').val();

	if(name == "" || gender == "" || position == "" || eployment_date == "" || empno == "")
		return;
	
	var data = {
		"empno": empno,
		"name": name,
		"gender": gender,
		"position": position,
		"eployment_date": eployment_date,
		"school_id": schoolID
	}; 
		
	var url;
	if(document.domain=="localhost")
	{
		url="http://localhost/lifeinclass/php/teacher.php";
	}else{
		url="php/teacher.php";
	}
	
	$.ajax({
	type: "POST",
	dataType: "json",
	url: url, 
	data: data,
	success: function(data) {
		if(data.error == "false"){
			$('#txtempno').val("");
			$('#txtTeacherName').val("");
			$('#txtTeacherGender').val("");
			$('#txtTeacherPosition').val("");
			$('#txtTeacherDoj').val("");
			getTeachers();
		}
	},
	error: function(e) {
		console.debug(e);
	}
	}); 
}

function getTeachers(){	
	var url="php/getTeacher.php";
	teachersTable.fnClearTable();
		
	$.ajax({
	url: url,
	type: 'GET',
	dataType: "json",
	data: 'school_id='+schoolID,
	success: function(data) {
		if(data.error=="false")
		{
			for(var x=0;x<(data.list.length);x++)
			{	
				teachersTable.fnAddData([
				data.list[x].id,
				data.list[x].empno,
				data.list[x].name,
				data.list[x].gender,
				data.list[x].position,
				data.list[x].eployment_date,
				'<center><button class="btn btn-info btn-xs" data-toggle="tooltip" data-placement="top" title="Edit Record"> <span class="glyphicon glyphicon-pencil"></span> </button> <button class="btn btn-danger btn-xs" onclick="deleteTeacher('+data.list[x].id+')" data-toggle="tooltip" data-placement="top" title="Delete Record"> <span class="glyphicon glyphicon-trash"></span> </button></center>'
				]);
			}		
		}
	},
	error: function(e) {
		console.debug("error");
	}
	});
}

function deleteTeacher(id){
	var data = {
		"id": id
	}; 
		
	var url;
	if(document.domain=="localhost")
	{
		url="http://localhost/lifeinclass/php/deleteTeacher.php";
	}else{
		url="php/deleteTeacher.php";
	}
	
	$.ajax({
	type: "POST",
	dataType: "json",
	url: url, 
	data: data,
	success: function(data) {
		if(data.error == "false"){
			getTeachers();
		}
	},
	error: function(e) {
		console.debug(e);
	}
	}); 
}

function updateTeacher(id)
{
	var data = {
		"id":id,
		"empno": $('#empno'+id).val(), 
		"name": $('#name'+id).val(), 
		"dob": $('#dob'+id).val(), 
		"eployment_date": $('#eployment_date'+id).val(), 
		"position": $('#position'+id).val(),
		"adhar": $('#adhar'+id).val(), 
		"gender": $('#gender'+id).val(), 
		"maritial_status": $('#maritial_status'+id).val(), 
		"cell_number": $('#cell_number'+id).val(), 
		"landline": $('#landline'+id).val(), 
		"email": $('#email'+id).val(), 
		"village": $('#village'+id).val(), 
		"city": $('#city'+id).val(), 
		"district": $('#district'+id).val(),
		"state": $('#state'+id).val(),
		"zip": $('#zip'+id).val(),
		"address": $('#address'+id).val(),
		"degree": $('#degree'+id).val(),
		"certificate": $('#certificate'+id).val(),
		"experience": $('#experience'+id).val(),
		"school_id": schoolID,
		"image":uploadedTeacherImage
	}; 
	
	var url;
	if(document.domain=="localhost")
	{
		url="http://localhost/lifeinclass/php/updateTeacher.php";
	}else{
		url="php/updateTeacher.php";
	}
	
	$.ajax({
	type: "POST",
	dataType: "json",
	url: url, 
	data: data,
	success: function(data) {
		if(data.error == "false"){			
			getTeachers();
		}
	},
	error: function(e) {
		console.debug(e);
	}
	}); 
	
}
